<?php

namespace Mobile\Controller;

class IndexController extends MobileCommonController{
	
	public function index(){
	/** 
	 * threenumber	
	 * 1:134  2:135  3:136  4:137  5:138  6:139  7:150  8:151  9:152  10:157  11:158  12:159  13:182  14:183  15:187  16:188  17:178
	 * category
	 * 1:不含四      2:后四位含四     3:后四位不含四    4:不含七     5:后四位含七     6:后四位不含七      7:不含四和七     8:后四位不含四和七
	 * special
	 * 1:520   2:521  3:ABAB   4:ABC  5:AABB   6:AAA  7:ABCD
	**/
		$p = I('p','');
		$lucky = I('lucky','',intval);
		$where['status'] = 0;
		$threenumber = I('threenumber','');//号段
		$category = I('category','');//号码特征
		$special = I('special','');//靓号
		
		$lucky = substr($lucky,1,1);
		
		if(!empty($lucky)){
			$where['number'] = array('like','%'.$lucky.'%');//幸运数字
		}
		
		if(!empty($threenumber)){
			$where['threenumber'] = $threenumber;
		}
		
		if(!empty($category)){
			$where['category'] = $category;
		}
		
		if(!empty($special)){
			$where['special'] = $special;
		}
		
		if($category == '1' || $category == '4' || $category == '8' || $category == '3'){
			$where['category'] = 7;
		}
		$count = M('cellphone') -> where($where) -> count();
		$page = new \Common\Lib\Page($count,9);
		$page->rollPage = 1;
		
		$page->setConfig('next','换一批');
		$page->setConfig('theme', '%DOWN_PAGE%');
		$limit = $page->firstRow. ',' .$page->listRows;
		$cellphone = M('cellphone')->where($where)->limit($limit) ->select();
		$pages = $page->show();
		$totalpage = $page->totalPages;
		if($p>=$totalpage){
			$page->setConfig('first','换一批');
			$page->setConfig('theme','%FIRST%  %DOWN_PAGE%');
			$limit = $page->firstRow. ',' .$page->listRows;
			$cellphone = M('cellphone')->where($where)->limit($limit) ->select();
			$pages = $page->show();
			$totalpage = $page->totalPages;
		}
		$this->lucky = $lucky; 
		$this->threenumber = $threenumber; 
		$this->category = $category;
		$this-> special = $special;
		$this->assign('page', $pages);
		$this->assign('cellphone', $cellphone);
		$this -> title = '志愿靓号随心选';
		$this->display();

	}
	
	// 留作外部调用 /index.php?s=/Index/cellphone/token/qinxuemobile
	public function cellphone(){
		$token = I('token','');
		if($token != 'qinxuemobile'){
			$this->error('Sorry, you have no right to access!');
		}
		$where['status'] = 1;
		$cellphone = M('cellphone')->where($where)->select();
		echo json_encode($cellphone);
	}
	
	public function str(){
		$length = 4;
		$chars = '0123456789';
		$str = '';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++) {
			$str .= $chars[mt_rand(0, $max)];
		}
		return $str;
		
	}
	
	public function send(){
			
		import("Org.Util.Send");
		$http = C('message.http');
		$uid = C('message.uid');
		$pwd = C('message.pwd');
		$mobile	 = I('number','');
		
		$mobileids	 = intval($mobile).microtime();
		$str = $this ->str();
		$_SESSION['vcode'] = $str;
		//$content = urlencode('【慧志愿】志愿靓号随心选验证码：'.$str.'，感谢您的使用');
		$content = urlencode('【慧志愿】慧志愿专家级高考志愿填报系统，您的验证码为:'.$str);
		$send = new \Send();
		$res = $send->sendSMS($http,$uid,$pwd,$mobile,$content,$mobileids);
		if (substr($res,9,11) == 100) {
			echo "<script>alert('发送成功，请注意查收短信');</script>";
		}else{
			echo "<script>alert('发送失败');</script>";
		}
		 
		$this->redirect("Index/Package/number/{$mobile}");
	}
	
	public function package(){
		$number = $_GET['number'];
		//查询数据库验证号码是否已经绑定
		$where['status'] = 1;
		$where['number'] = $number;
		$result = M('cellphone')->where($where)->find();
		if(!empty($result)){
			$this->error('下手慢啦，刚刚有人抢走喽!');
		}
		$this->number = $number;
		$this->display();
	}
	
	public function person(){
		
		$number = I('number','');
		$payment = I('payment','');
		$package = I('package','');
		if(empty($package)){
			$package = '4G流量卡50元套餐';
		}
		$this->number  = $number;
		$this->payment = $payment;
		$this->package = $package;
		$this ->display();
	}
	
	public function postperson(){
		$where['number'] = I('number','');
		$data['payment'] = I('payment','');
		$data['package'] = I('package','');
		$data['username'] = I('username','');
		$data['contactphone'] = I('contactphone','');
		$data['idcard'] = I('idcard','');
		$data['hall'] = I('hall','');
		$vcode = I('vcode','');
		$str = $_SESSION['vcode'];
		 if(empty($data['username'])){
			$this->error('请填写联系人！');
		}
		if(empty($vcode)){
			$this->error('请填写验证码！');
		}
		if(empty($data['idcard'])){
			$this->error('请填写身份证！');
		}
		if(empty($data['hall'])){
			$this->error('请选择营业厅！');
		}
		if($vcode != $str){
			$this->error('验证码失效！');
		} 
		$data['statustime'] = date('Y-m-d H:i:s', time());
		$data['status'] = 1;
		$result = M('cellphone') -> where($where) ->save($data);
		
		if(empty($result)){
			$this->error('提交失败，请重新提交！','index',3);
		}else{
			$this->suc();
		}
	}
	
	public function suc(){
		$number = I('number','');
		$payment = I('payment','');
		$package = I('package','');
		$this-> number = $number;
		$this-> payment = $payment;
		$this-> package = $package;
		$this-> username= $username;
		$this-> idcard = $idcard;
		$this-> hall = $hall;
		$this->display('suc');
	}
}

?>